﻿package com.duartepeixinho
{
	import com.duartepeixinho.Math.Matrix;
	import flash.display.Sprite;
	import flash.events.Event;
	import com.duartepeixinho.Math.Vector3;
	import flash.geom.Matrix3D;
	import flash.geom.Point;
	/**
	 * ...
	 * @author Duarte Peixinho
	 */
	public class Main extends Sprite 
	{
				
		private var p0:Vector3;
		private var p1:Vector3;
		private var p2:Vector3;
		private var p3:Vector3;
		private var p4:Vector3;
		private var p5:Vector3;
		private var p6:Vector3;
		private var p7:Vector3;
		private var container:Sprite;
		private var rotY:Number = Math.PI * .25;				
		private var sinCurve:Number=0;
		public function Main():void 
		{			
			
			p0 = new Vector3(-50, -50, -50);
			p1 = new Vector3(50, -50, -50);
			p2 = new Vector3(50, 50, -50);
			p3 = new Vector3(-50, 50, -50);
			p4 = new Vector3(-50, -50, 50);
			p5 = new Vector3(50, -50, 50);
			p6 = new Vector3(50, 50, 50);
			p7 = new Vector3(-50, 50, 50);
			container = new Sprite();
			
			container.x = 275;
			container.y = 200;
			addChild(container);
			addEventListener(Event.ENTER_FRAME, render);
			
		}
		private function render(e:Event):void {
			rotY += Math.PI/180;
			var matrix:Matrix = new Matrix();
			matrix.rotateY(rotY);
			matrix.rotateZ(rotY);
			matrix.scale(1, 1, 2);
			sinCurve += Math.PI/180;			 
			var xPos:Number = Math.sin(sinCurve) * 150;
			matrix.translate(xPos, 0, 0);
			
			var point0:Point = getPerspective(matrix.transform(p0));
			var point1:Point = getPerspective(matrix.transform(p1));
			var point2:Point = getPerspective(matrix.transform(p2));
			var point3:Point = getPerspective(matrix.transform(p3));
			var point4:Point = getPerspective(matrix.transform(p4));
			var point5:Point = getPerspective(matrix.transform(p5));
			var point6:Point = getPerspective(matrix.transform(p6));
			var point7:Point = getPerspective(matrix.transform(p7));
			
			container.graphics.clear();		 
			container.graphics.lineStyle(0, 0xFF0000);
			
			container.graphics.moveTo(point0.x,point0.y);
			container.graphics.lineTo(point1.x,point1.y);
			container.graphics.lineTo(point2.x,point2.y);
			container.graphics.lineTo(point3.x,point3.y);
			container.graphics.lineTo(point0.x,point0.y);
		 
			container.graphics.moveTo(point4.x,point4.y);
			container.graphics.lineTo(point5.x,point5.y);
			container.graphics.lineTo(point6.x,point6.y);
			container.graphics.lineTo(point7.x,point7.y);
			container.graphics.lineTo(point4.x,point4.y);
		 
			container.graphics.moveTo(point0.x,point0.y);
			container.graphics.lineTo(point4.x,point4.y);
		 
			container.graphics.moveTo(point1.x,point1.y);
			container.graphics.lineTo(point5.x,point5.y);
		 
			container.graphics.moveTo(point2.x,point2.y);
			container.graphics.lineTo(point6.x,point6.y);
		 
			container.graphics.moveTo(point3.x,point3.y);
			container.graphics.lineTo(point7.x,point7.y);
		}
		private function getPerspective(vector:Vector3):Point {
			var focalLength:Number = 1000;
			var scaleFactor:Number = focalLength / (focalLength + vector.z);
			
			var point:Point = new Point();
			point.x = vector.x * scaleFactor;
			point.y = vector.y * scaleFactor;
			
			return point;
		}
		
	}
	
}